package com.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.search.common.dto.PageDTO;
import com.search.common.dto.client.ItemClient;
import com.search.common.dto.pojo.Item;
import com.search.constans.EsItemConstant;
import com.search.pojo.ItemDoc;
import com.search.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;


@Service
@Slf4j
public class ItemServiceImpl implements ItemService {
    @Autowired
    RestHighLevelClient restHighLevelClient;
    @Autowired
    ItemClient itemClient;

    @Override
    public void importItem(){
        int sign=0;
        while (true){
            PageDTO<Item> list = itemClient.queryById(sign, 2000);
            List<Item> items = list.getList();
            if (items==null || items.size()==0){
                return;
            }
            BulkRequest bulkRequest = new BulkRequest();
            // 批量导入到es
            for (Item i : items) {
                //构造es中的ItemDoc对象
                ItemDoc itemDoc = new ItemDoc(i);
                //把添加索引的IndexRequest 添加到 bulkRequest中去
                IndexRequest indexRequest = new IndexRequest(EsItemConstant.INDEX_NAME).id(String.valueOf(i.getId()));
                indexRequest.source(JSON.toJSONString(itemDoc), XContentType.JSON);
                //把“新增文档请求对象”封装到“批量请求对象”中
                bulkRequest.add(indexRequest);
            }
            try {
                // 批量发送给服务端执行
                restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                log.info("第{}次，批量导入成功", sign++);
            } catch (IOException e) {
                log.info("第"+sign+"次导入失败");
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean saveBatch(Collection<Item> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Item> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Item> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Item entity) {
        return false;
    }

    @Override
    public Item getOne(Wrapper<Item> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Item> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Item> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<Item> getBaseMapper() {
        return null;
    }

    @Override
    public Class<Item> getEntityClass() {
        return null;
    }
}

